<template>
  <a-card :bordered='false'>
    <!-- 查询区域 -->
    <div class='table-page-search-wrapper'>
      <a-form layout='inline' @keyup.enter.native='searchQuery'>
        <a-row :gutter='24'>
          <a-col :xl='6' :lg='7' :md='8' :sm='24'>
            <a-form-item label='月份'>
              <a-input placeholder='请输入月份' v-model='salTime' />
            </a-form-item>
          </a-col>
          <a-col :xl='6' :lg='7' :md='8' :sm='24'>
            <span style='float: left;overflow: hidden;' class='table-page-search-submitButtons'>
              <a-button type='primary' @click='searchQuery' icon='search'>查询</a-button>
              <a-button type='primary' @click='searchReset' icon='reload' style='margin-left: 8px'>重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- table区域-begin -->
    <div>
      <a-table
        ref='table'
        size='middle'
        :scroll='{x:true}'
        bordered
        :rowKey='record=>{return record.salTime}'
        :columns='columns'
        :dataSource='dataSource'
        :pagination='ipagination'
        :loading='loading'
        class='j-table-force-nowrap'
        @change='handleTableChange'>
        <template slot='htmlSlot' slot-scope='text'>
          <div v-html='text'></div>
        </template>
        <template slot='monthSalary' slot-scope='text, record'>
          <span>{{ (record.personalIncome + record.partSalary + record.personalSecurity + record.personalProvident + record.companySecurity + record.companyProvident).toFixed(2)
            }}</span>
        </template>
        <template slot='moneyFlow' slot-scope='text, record'>
          <span>{{ (record.arrivalMoney - record.partSalary - record.personalIncome - record.personalSecurity - record.personalProvident - record.companySecurity - record.companyProvident - record.reimbursementTotal - record.purchaseMoney - record.othersMoney).toFixed(2)
            }}</span>
        </template>
        <template slot='imgSlot' slot-scope='text'>
          <span v-if='!text' style='font-size: 12px;font-style: italic;'>无图片</span>
          <img v-else :src='getImgView(text)' height='25px' alt=''
               style='max-width:80px;font-size: 12px;font-style: italic;' />
        </template>
        <template slot='fileSlot' slot-scope='text'>
          <span v-if='!text' style='font-size: 12px;font-style: italic;'>无文件</span>
          <a-button
            v-else
            :ghost='true'
            type='primary'
            icon='download'
            size='small'
            @click='downloadFile(text)'>
            下载
          </a-button>
        </template>
      </a-table>
      <a-row>
        <a-col :span='6'>
          <a-card style='text-align:left;line-height:30px'>
            <a-statistic title='合同金额总和（元）' :value='sumMoney.toFixed(2)' />
          </a-card>
        </a-col>
        <a-col :span='6'>
          <a-card style='text-align:left;line-height:30px'>
            <a-statistic title='回款总和（元）' :value='sumReturnMoney.toFixed(2)' />
          </a-card>
        </a-col>
        <a-col :span='6'>
          <a-card style='text-align:left;line-height:30px'>
            <a-statistic title='工资总和（元）' :value='sumSalary.toFixed(2)' />
          </a-card>
        </a-col>
        <a-col :span='6'>
          <a-card style='text-align:left;line-height:30px'>
            <a-statistic title='报销总和（元）' :value='sumReimbursementTotal.toFixed(2)' />
          </a-card>
        </a-col>

        <a-col :span='6'>
          <a-card style='text-align:left;line-height:30px'>
            <a-statistic title='采购总和（元）' :value='sumPurchaseMoney.toFixed(2)' />
          </a-card>
        </a-col>
        <a-col :span='6'>
          <a-card style='text-align:left;line-height:30px'>
            <a-statistic title='待回款总和（元）' :value='avgMoneySal.toFixed(2)' />
          </a-card>
        </a-col>
        <a-col :span='6'>
          <a-card style='text-align:left;line-height:30px'>
            <a-statistic title='现金流（元）' :value='sumMoneyFlow.toFixed(2)' />
          </a-card>
        </a-col>
        <a-col :span='6'>
          <a-card style='text-align:left;line-height:30px'>
            <a-statistic title='利润总额（元）' :value='sumProfit.toFixed(2)' />
          </a-card>
        </a-col>
      </a-row>
    </div>

    <hf-ps-personal-check-modal ref='modalForm' @ok='modalFormOk'></hf-ps-personal-check-modal>
  </a-card>
</template>

<script>

import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import HfPsPersonalCheckModal from './modules/HfPsPersonalCheckModal'
import { sorter } from '@/utils/sorter'
import { getAction } from '@api/manage'
import { toFixedTwo } from '@/utils/util'

export default {
  name: 'HfPsPersonalCheckList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    HfPsPersonalCheckModal
  },
  data() {
    return {
      description: '中心核算管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '公司',
          align: 'center',
          dataIndex: 'auditGrade_dictText',
          customRender: (text) => {
            return '华天海峰'
          }

        },
        {
          title: '月份',
          align: 'center',
          sorter: true,
          dataIndex: 'salTime'
        },
        {
          title: '合同金额',
          align: 'right',
          dataIndex: 'money',
          customRender: (text) => {
            return !text ? '0' : String(text.toFixed(2)).replace(/\B(?=(\d{3})+(?!\d))/g, ',')
          }
        },
        {
          title: '项目毛利',
          align: 'right',
          dataIndex: 'actualProfit',
          customRender: (text) => {
            return !text ? '0' : String(text.toFixed(2)).replace(/\B(?=(\d{3})+(?!\d))/g, ',')
          }
        },
        {
          title: '月度回款',
          align: 'right',
          dataIndex: 'arrivalMoney',
          customRender: (text) => {
            return !text ? '0' : String(text.toFixed(2)).replace(/\B(?=(\d{3})+(?!\d))/g, ',')
          }

        },
        {
          title: '工资费用',
          align: 'right',
          dataIndex: 'monthSalary',
          scopedSlots: { customRender: 'monthSalary' }
        },
        {
          title: '报销报销',
          align: 'right',
          dataIndex: 'reimbursementTotal',
          customRender: (text) => {
            return !text ? '0' : String(text.toFixed(2)).replace(/\B(?=(\d{3})+(?!\d))/g, ',')
          }
        },
        {
          title: '采购费用',
          align: 'right',
          dataIndex: 'purchaseMoney',
          customRender: (text) => {
            return !text ? '0' : String(text.toFixed(2)).replace(/\B(?=(\d{3})+(?!\d))/g, ',')
          }
        },
        {
          title: '其他费用',
          align: 'right',
          dataIndex: 'othersMoney',
          customRender: (text) => {
            return !text ? '0' : String(text.toFixed(2)).replace(/\B(?=(\d{3})+(?!\d))/g, ',')
          }
        },
        {
          title: '现金流',
          align: 'right',
          dataIndex: 'moneyFlow',
          scopedSlots: { customRender: 'moneyFlow' }
        }

      ],
      url: {
        list: '/ps/hfPsSalary/compCheckList'
      },
      dictOptions: {},
      superFieldList: [],
      dataSource1: [],
      salTime: '',
      sumMoney: 0,
      sumPurchaseMoney: 0,
      sumProfit: 0,
      sumMoneyFlow: 0,
      sumSalary: 0,
      avgMoneySal: 0,
      sumReturnMoney: 0,
      sumReimbursementTotal: 0
    }
  },
  created() {
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    searchQuery() {
      if (this.salTime == '') return
      let arr = []
      this.dataSource1.forEach(item => {
        if (item.salTime == this.salTime) {
          arr.push(item)
        }
      })
      this.dataSource = arr
      this.count()
    },
    searchReset() {
      this.queryParam = {}
      this.salTime = ''
      this.loadData(1)
    },
    count() {
      this.sumPurchaseMoney = 0
      this.sumSalary = 0
      this.sumMoney = 0
      this.sumProfit = 0
      this.sumMoneyFlow = 0
      this.avgMoneySal = 0
      this.sumReturnMoney = 0
      this.sumReimbursementTotal = 0
      this.dataSource.forEach(item => {
        this.sumMoney += item.money
        this.sumSalary += item.partSalary + item.personalIncome + item.personalSecurity + item.personalProvident + item.companySecurity + item.companyProvident
        this.sumReimbursementTotal += item.reimbursementTotal
        this.sumPurchaseMoney += item.purchaseMoney
        this.sumReturnMoney += item.arrivalMoney
        this.sumMoneyFlow += item.arrivalMoney  - item.partSalary - item.personalIncome - item.personalSecurity - item.personalProvident - item.companySecurity - item.companyProvident - item.reimbursementTotal - item.purchaseMoney - item.othersMoney
        this.avgMoneySal += item.money - item.arrivalMoney
      })
    },
    loadData() {
      this.sumPurchaseMoney = 0
      this.sumSalary = 0
      this.sumMoney = 0
      this.sumProfit = 0
      this.sumMoneyFlow = 0
      this.avgMoneySal = 0
      this.sumReturnMoney = 0
      this.sumReimbursementTotal = 0
      var params = this.getQueryParams()//查询条件
      this.loading = true
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          this.dataSource = res.result.records
          this.ipagination.total = res.result.total
          this.loadData1()
        } else {
          this.$message.warning(res.message)
        }
        this.loading = false
      })
    },
    loadData1() {
      this.sumPurchaseMoney = 0
      this.sumSalary = 0
      this.sumMoney = 0
      this.sumProfit = 0
      this.sumMoneyFlow = 0
      this.avgMoneySal = 0
      this.sumReturnMoney = 0
      this.sumReimbursementTotal = 0
      var params = this.getQueryParams()//查询条件
      params.pageNo = -1
      params.pageSize = -1
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          this.dataSource1 = res.result.records
          res.result.records.forEach(item => {
            this.sumMoney += item.money
            this.sumSalary += item.partSalary + item.personalIncome + item.personalSecurity + item.personalProvident + item.companySecurity + item.companyProvident
            this.sumReimbursementTotal += item.reimbursementTotal
            this.sumPurchaseMoney += item.purchaseMoney
            this.sumReturnMoney += item.arrivalMoney
            this.sumMoneyFlow += item.arrivalMoney  - item.partSalary - item.personalIncome - item.personalSecurity - item.personalProvident - item.companySecurity - item.companyProvident - item.reimbursementTotal - item.purchaseMoney - item.othersMoney
            this.avgMoneySal += item.money - item.arrivalMoney
          })
        }
      })
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>